package com.stropheware.droidDefense;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;

public class AbstractProjectile {

	protected Drawable drawable;
	protected AnimationDrawable animationDrawable;
	protected int x, y;
	protected int speed; // in terms of pixel speed
	public AbstractEnemy targetEnemy;
	protected GameBoard gameBoard;
	protected AbstractTower tower;
	protected Context context;
	private Paint projectilePaint;
	
	public AbstractProjectile(GameBoard gameBoard, AbstractTower tower, Context context) {
		this.gameBoard = gameBoard;
		this.tower = tower;
		this.context = context;
		targetEnemy = null;
		projectilePaint = new Paint();
		projectilePaint.setAntiAlias(true);
		projectilePaint.setColor(Color.RED);
	}
	
	public void fireAtEnemy(AbstractEnemy targetEnemy) {
		this.targetEnemy = targetEnemy;
		x = tower.centerX;
		y = tower.centerY;
	}
	
	public void update(int tickCounter) {
		if(targetEnemy != null && tickCounter % speed == 0) {
			if(!targetEnemy.visible) targetEnemy = null; //enemy got killed by someone else
			else {
				if(targetEnemy.getProjectileX() > x) x++;
				else if(targetEnemy.getProjectileX() < x) x--;
				if(targetEnemy.getProjectileY() > y) y++;
				else if(targetEnemy.getProjectileY() < y) y--;
				if(targetEnemy.getProjectileX() == x && targetEnemy.getProjectileY() == y) {
					targetEnemy.hit(tower.damagePerShot);
					targetEnemy = null;
				}
			}
		}
	}
	
	public void draw(Canvas canvas) {
		if(targetEnemy != null) {
			canvas.drawCircle(x, y, 3, projectilePaint);
		}
	}
}
